﻿using aier.dataaccess.dapper;
using aier.dataaccess.model;
using aier.xrm.wcf;
using log4net;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace aier.business.logic
{
    public class ScreenPropagandaBll
    {
        public ILog log { get; set; }
        public CommonWcf commonWcf { get; set; }
        public ResResult resResult { get; set; }
        public ScreenPropagandaDal screenPropagandaDal { get; set; }

        #region Update 更新
        /// <summary>
        /// 创建宣教活动
        /// </summary>
        /// <param name="propaganda">筛查宣教对像</param>
        /// <returns></returns>
        public ResResult CreatePropaganda(ScreenPropaganda propaganda)
        {
            Guid entityId = Guid.Empty;
            Entity entity = propaganda.AsCrmEntity();

            try
            {
                if (Guid.Empty.Equals(propaganda.userid))
                {
                    entityId = commonWcf.CreateEntity(entity);
                }
                else
                {
                    if (propaganda.userid != propaganda.OwnerId)
                        commonWcf.CreateEntity(entity);
                    else
                        entityId = commonWcf.CreateEntityProxy(propaganda.userid, entity);
                }

                return resResult.SuccessResult(entityId);
            }
            catch (Exception ex)
            {
                log.Error("ScreenPropagandaBll-> CreatePropaganda: " + ex.Message + ":" + Newtonsoft.Json.JsonConvert.SerializeObject(propaganda));
                return resResult.FailResult("ScreenPropagandaBll-> CreatePropaganda: " + ex.Message);
            }
        }

        /// <summary>
        /// 更新筛查宣教
        /// </summary>
        /// <param name="propaganda">筛查宣教对像</param>
        /// <returns></returns>
        public ResResult UpdatePropaganda(ScreenPropaganda propaganda)
        {
            try
            {
                Entity entity = propaganda.AsCrmEntity();
                if (Guid.Empty.Equals(propaganda.userid))
                {
                    commonWcf.UpdateEntityById(entity);
                }
                else
                {
                    commonWcf.UpdateEntityProxyById(propaganda.userid, entity);
                }

                return resResult.SuccessResult();
            }
            catch (Exception ex)
            {
                log.Error("ScreenPropagandaBll-> UpdatePropaganda: " + ex.Message);
                return resResult.FailResult("ScreenPropagandaBll-> UpdatePropaganda: " + ex.Message);
            }
        }

        #endregion

        #region 查询 

        /// <summary>
        /// 根据筛查ID，潜客ID查询筛查宣教信息
        /// 原程序方法：ScreeningServiceImpl-> queryPreach(String scid, String customerid)
        /// <returns></returns>
        public IEnumerable<ScreenPropagandaView> QueryPropagandaByScidAndLeadId(Guid scid, Guid leadid)
        {
            return screenPropagandaDal.QueryPropagandaByScidAndLeadId(scid, leadid);
        }

        #endregion

    }
}
